// pages/portfolio/portfolio.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName:'',
    avatar:'',
    portfolio:[],
    portfolio_origin:[],
    addflag:true,  // 判断是否显示搜索框右侧部分
    addimg:'/images/activity_add.png',
    searchstr:'',
    loadingHide:true, // 添加加载中动画显示判断
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let nickName = wx.getStorageSync('nickName')
    let avatarUrl = wx.getStorageSync('avatar')
    let portfolio = []
    let userid = wx.getStorageSync('userid')
    let that = this
    wx.request({
      url: 'https://thinker.skyseaee.com/getPortfolio',
      data:{
        userid:userid
      },
      success(res){
        portfolio = res.data.portfolio
        //console.log(portfolio)
        let tmpArr = portfolio
        for(let tmp of tmpArr) {
          let tmpPath = tmp.cover.split('.')[1]
          tmp.cover = "https://thinker.skyseaee.com/initCover/?fileName="+tmp.orderID+'.'+tmpPath
        }
        that.setData({
          nickName:nickName,
          avatar:avatarUrl,
          portfolio:tmpArr,
          portfolio_origin:tmpArr
        })
        //console.log(that.data.portfolio)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      loadingHidden: false
    });
    var that = this;
    setTimeout(function(){
      that.setData({
          loadingHidden: true
      });
    }, 3000);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  checkDetail: function(event){
    //console.log(event.currentTarget.dataset.item)
    // let cover =  this.data.portfolio[event.currentTarget.dataset.index].cover
    // let title =  this.data.portfolio[event.currentTarget.dataset.index].title
    // let musicAuto = this.data.portfolio[event.currentTarget.dataset.index].info
    //console.log(cover)
    let app = getApp()
    app.globalData.hasSource = 1
    app.globalData.currentMusicId = this.data.portfolio[event.currentTarget.dataset.index].orderID
    wx.switchTab({
      url: '/pages/musicInfo/musciInfo',
      success(res){
        //console.log(that.data.portfolio[event.currentTarget.dataset.index])
      }
    })
  },

  tap(e) {

  },

  // 搜索框右侧 事件
  addhandle() {
    wx.showModal({
      title: '创作',
      content: '是否前往创作白噪音页面',
      success (res) {
        if (res.confirm) {
          wx.switchTab({
            url: '/pages/createZone/createZone',
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  },

  //搜索框输入时触发
  searchList(ev) {
    let e = ev.detail;
    this.setData({
      searchstr: e.detail.value
    })
  },
  //搜索回调
  endsearchList(e) {
    // console.log('查询数据')
    let port = this.data.portfolio
    port = port.filter(item=>item.title.indexOf(this.data.searchstr)!=-1)
    this.setData({
      portfolio:port
    })
  },
  // 取消搜索
  cancelsearch() {
    this.setData({
      searchstr: '',
      portfolio:this.data.portfolio_origin
    })
  },
  //清空搜索框
  activity_clear(e) {
    this.setData({
      searchstr: '',
      portfolio:this.data.portfolio_origin
    })
  },
})